//const url = 'http://localhost:8099/haopyYueAi/home/collect';

const rootUrl = 'https://eval.nanone.org.cn/chargeApi/haopyYueAi/';

var chargeId;
var token;


$(function () {
    chargeId = new URLSearchParams(window.location.search).get('chargeId');
    token = new URLSearchParams(window.location.search).get('token');
    queryCollectFun();

    $("#copy_div").hide();
    $("#process").hide();

});


function addCollectFun() {

    $.get({
        url: rootUrl + "standardCharges/collect",
        headers: {
            'NanOneChargeComputeToken': token,
        },
        data: {
            'chargeId': chargeId,
        },
        method: 'GET',
        dataType: 'json',
        timeout: 5000,
        success: function (res) {
            //console.log(res)
            if (res.errno == 0) {
                if (res.data.isCollect) {
                    $("#collect_img").attr("src", '../img/collect.png');
                    $("#collect_txt").html("取消");
                    $("#collect_txt").addClass("collect_success");
                } else {
                    $("#collect_img").attr("src", '../img/un_collect.png');
                    $("#collect_txt").html("收藏");
                    $("#collect_txt").removeClass("collect_success");
                }
            } else {
                alert(res.errmsg)
            }
        },
        error: function (xhr, status, error) {
            console.error(error)
        },
        complete: function () {
            console.log("finish")
        }
    });
}

function queryCollectFun() {
    $.get({
        url: rootUrl + "standardCharges/collectQuery",
        headers: {
            'NanOneChargeComputeToken': token,
        },
        data: {
            'chargeId': chargeId,
        },
        method: 'GET',
        dataType: 'json',
        timeout: 5000,
        success: function (res) {
            //console.log(res)
            if (res.errno == 0) {
                if (res.data.isCollect) {
                    $("#collect_img").attr("src", '../img/collect.png');
                    $("#collect_txt").html("取消");
                    $("#collect_txt").addClass("collect_success");
                }
            } else {
                alert(res.errmsg)
            }
        },
        error: function (xhr, status, error) {
            console.error(error)
        },
        complete: function () {
            console.log("finish")
        }
    });
}


/**
 * 复制计算过程
 */
function copyProcessFun(str) {
    var content = $("#process").html();
    content = content.replaceAll("<br>", "\n").replace('<h3>', "").replace("</h3>", "\n");
    console.log(content)
    content += " \n" + str;
    navigator.clipboard.writeText(content).then(function () {
        alert("复制成功")
    }).catch(function (err) {
        alert()
        console.error('复制失败', err);
    });
}


/**
 * 显示计算过程
 */
function showProcess() {
    $("#copy_div").show();
    $("#process").show();
}

